package com.example.demo.contorller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.Bean.*;
import com.example.demo.Utils.ExcelUtil;
import com.example.demo.service.ExperimentManagementService;
import com.example.demo.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: Redi
 * @Date: 2021/8/31 10:37
 * @Description: 实验管理控制器
 */
@RestController
@RequestMapping("/experiment_management")
public class ExperimentManagementController {

    @Autowired
    ExperimentManagementService experimentService;

    @Autowired
    ReportService reportService;

    @GetMapping("/getDetail")
    @CrossOrigin
    public Result getExperimentDetail(Integer experiment_id){
        try {
            return experimentService.getExperimentDetail(experiment_id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/modifyDetail")
    @CrossOrigin
    public Result modifyExperimentDetail(Experiment experiment){
        try {
            return experimentService.modifyExperimentDetail(experiment);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/add")
    @CrossOrigin
    public Result addExperiment(Experiment experiment){
        try {
            return experimentService.addExperiment(experiment);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/search")
    @CrossOrigin
    public Result searchExperiment(Integer pageNum, Integer pageSize,String name){
        try {
            return experimentService.searchExperiment(pageNum, pageSize, name);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/delete")
    @CrossOrigin
    public Result deleteExperiment(Integer experimentId){
        try {
            return experimentService.deleteExperiment(experimentId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/getPage")
    @CrossOrigin
    public Result getExperimentPage(Integer pageNum,Integer pageSize, String courseId,String name){
        try {
            return experimentService.getPage(pageNum, pageSize, courseId, name);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/getTotalNum")
    @CrossOrigin
    public Result getTotalExperimentNum(){
        try {
            return experimentService.getTotalExperimentNum();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/getSubmittedInfo")
    @CrossOrigin
    public Result getSubmittedInfo(Integer experiment_id){
        try {
            return experimentService.getSubmittedNums(experiment_id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/get_submitted_reports")
    @CrossOrigin
    public Result getSubmittedReports(Integer pageNum,Integer pageSize,Integer experiment_id){
        try {
            return experimentService.getSubmittedReports(pageNum, pageSize, experiment_id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/grade_reports")
    @CrossOrigin
    public Result gradeReports(Integer report_id,float code,String comment){
        try {
            return experimentService.reportGrade(report_id, code, comment);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @PostMapping("/getReportsExcel")
    @CrossOrigin
    public Result getReportsExcel(Integer id, HttpServletResponse response){
        try {
            QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("experiment_id", id);
            List<Report> reports = reportService.selectList(queryWrapper);
            try {
                ExcelUtil.download(response, Report.class, reports);
            } catch (IOException | InstantiationException | IllegalAccessException e) {
                return new Result(500, e.getMessage());
            }
            return new Result(200, "");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/saveAsDraft")
    @CrossOrigin
    public Result saveAsDraft(Drafts drafts){
        try {
            return experimentService.saveAsDraft(drafts);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @PostMapping("/publish")
    @CrossOrigin
        public Result publishTask(@RequestBody Map<String, Object> params){
        try {
            String json = null;
            json = JSON.toJSONString(params.get("courseId"));
            String courseId = JSON.parseObject(json, String.class);
            json = JSON.toJSONString(params.get("draftId"));
            Integer draftId = JSON.parseObject(json, Integer.class);
            json = JSON.toJSONString(params.get("groupIds"));
            List<Integer> groupIds = JSON.parseObject(json, ArrayList.class);
            return experimentService.publishTask(courseId, draftId, groupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/draftUpdate")
    @CrossOrigin
    public Result draftUpdate(Drafts drafts){
        try {
            return experimentService.draftUpdate(drafts);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/draftDelete")
    @CrossOrigin
    public Result draftDelete(Integer draftId){
        try {
            return experimentService.draftDelete(draftId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @PostMapping("/draftSearch")
    @CrossOrigin
    public Result draftSearch(Integer pageNum,Integer pageSize,String name,String teacherUid){
        try {
            return experimentService.draftSearch(name, pageNum, pageSize, teacherUid);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/reports_search")
    @CrossOrigin
    public Result reportsSearch(Integer pageNum,Integer pageSize,String name,Integer experimentId){
        try {
            return experimentService.selectByName(name, pageNum, pageSize, experimentId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @GetMapping("/modifyScale")
    @CrossOrigin
    public Result scaleModify(Integer experimentId,Double scale){
        try {
            return experimentService.experimentScaleModify(experimentId, scale);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }
    
    @GetMapping("/published")
    @CrossOrigin
    public Result published(Integer draftId, String courseId){
        try {
            return experimentService.published(draftId, courseId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }


}
